'use strict';

const Service = require('egg').Service;

class StudentService extends Service {
    async list(pagenum,limit,username,num,classNum) {
        let startIndex = (pagenum-1)*limit;
        let sql = 'select * from studentlist';
        if(username || num || classNum){
            sql += ' where'
        }
        sql += username?` username='${username}'`:'';

        if(username && num){
            sql+=' and'
        }

        sql += num?` num='${num}'`:'';

        if((username || num) && classNum){
            sql+=' and'
        }

        sql += classNum ? ` classNum='${classNum}'`:'';

        sql += ` limit ${startIndex},${limit}`;

        console.log(sql);
        
        return await this.app.mysql.query(sql);
    }
    //删除
    async del(id){
        await this.app.mysql.query('delete from studentlist where id=?',[id])
    }
    //获取总条数
    async total(username,num,classNum){
        let sql = 'select count(*) from studentlist';
        if(username || num || classNum){
            sql += ' where'
        }
        sql += username?` username='${username}'`:'';

        if(username && num){
            sql+=' and'
        }

        sql += num?` num='${num}'`:'';

        if((username || num) && classNum){
            sql+=' and'
        }

        sql += classNum ? ` classNum='${classNum}'`:'';
        return await this.app.mysql.query(sql)
    }
}

module.exports = StudentService;
